package com.jianghu;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.jianghu.helpers.GetLineFromtxt;
import com.jianghu.helpers.TrendsShow;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Articlesliked extends Activity {
	public Context context = Articlesliked.this;
	DownloadMore downloadMore = null;
	public Boolean surplus = true;
	ImageButton back_ibtn, home_ibtn, left_ibtn, right_ibtn;
	GridView gridView;
	ProgressBar progressBar;
	TextView content, likedcount, username;
	ImageView userimage;
	ImageAndTextListAdapter listItemAdapter = null;
	List<TrendsShow> dataArray = null;
	LinearLayout loadingLayout = null;
	LinearLayout articlesliked_layout;
	Timer timer;
	PopupWindow pop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.articlesliked);
		Init();
		// Intent intent = getIntent();
		// Bundle bundle = intent.getExtras();
		// String title = bundle.get("title").toString();
		// String contentStr = bundle.get("content").toString();
		// String count = bundle.get("count").toString();
		// username.setText(title);
		// content.setText(contentStr);
		// likedcount.setText(count);
		dataArray = new ArrayList<TrendsShow>();
		downloadMore = new DownloadMore();
		downloadMore.execute((Void) null);
	}

	private void Init() {
		back_ibtn = (ImageButton) findViewById(R.id.backbutton);
		home_ibtn = (ImageButton) findViewById(R.id.homebutton);
		username = (TextView) findViewById(R.id.username);
		likedcount = (TextView) findViewById(R.id.comment_count);
		content = (TextView) findViewById(R.id.comment_content);
		gridView = (GridView) findViewById(R.id.gridViewOfComment);
		progressBar = (ProgressBar) findViewById(R.id.comm_progressbar);
		left_ibtn = (ImageButton) findViewById(R.id.btnleft);
		right_ibtn = (ImageButton) findViewById(R.id.btnright);
		articlesliked_layout = (LinearLayout) findViewById(R.id.articlesliked_layout);
		View view = getLayoutInflater().inflate(R.layout.popwindow, null);
		pop = new PopupWindow(view, LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT);

		back_ibtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Articlesliked.this.finish();
			}
		});
		home_ibtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(Articlesliked.this, MainPage.class);
				Articlesliked.this.finish();
				Articlesliked.this.startActivity(intent);
			}
		});
		articlesliked_layout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				pop.showAtLocation(articlesliked_layout, Gravity.CENTER, 0,
						2000);
				timer = new Timer();
				timer.schedule(new TimerTask() {
					@Override
					public void run() {
						pop.dismiss();
					}
				}, 10000);
			}
		});
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent();
				intent.setClass(Articlesliked.this, UserTrendsManual.class);
				Bundle bundle = new Bundle();
				bundle.putString("item", "设计工厂");
				bundle.putString("type", "trends");
				intent.putExtras(bundle);
				Articlesliked.this.startActivity(intent);
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (timer != null) {
				timer.cancel();
			}
			if (pop != null && pop.isShowing()) {
				pop.dismiss();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

	private void loadRemnantListItem(int totalItemCount) {
		int max = totalItemCount + 5;
		for (int i = totalItemCount; i < max; i++) {

			String str = GetLineFromtxt.readAppointedLineNumber(
					Articlesliked.this, i + 1);
			if (str == null) {
				surplus = false;
				break;
			}
			String imageUrl = "http://t3.baidu.com/it/u=3744283480,376574923&fm=0&gp=0.jpg";
			String username = "冷笑话精选";
			String time = i + "小时前";
			String contents = str;
			TrendsShow trendsShow = new TrendsShow(imageUrl, username, time,
					contents);
			dataArray.add(trendsShow);
		}
	}

	class DownloadMore extends AsyncTask<Void, Void, Void> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progressBar.setVisibility(View.VISIBLE);
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			loadRemnantListItem(5);
			for (int i = 0; i < 5; i++) {
				String str = GetLineFromtxt.readAppointedLineNumber(
						Articlesliked.this, i + 1);
				if (str == null) {
					surplus = false;
					break;
				}
				String imageUrl = "http://www.touxiang.cn/downimage/20061119100612/20061119100612(6)_200611191006126.jpg";
				String username = "时尚英语";
				String time = i + "小时前";
				String contents = str;
				TrendsShow trendsShow = new TrendsShow(imageUrl, username,
						time, contents);
				dataArray.add(trendsShow);
			}
			// Log.v("dataarraty", dataArray.size() + " " + surplus);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// listItemAdapter.notifyDataSetChanged();
			listItemAdapter = new ImageAndTextListAdapter(Articlesliked.this,
					dataArray, gridView);
			gridView.setAdapter(listItemAdapter);
			progressBar.setVisibility(View.INVISIBLE);
			super.onPostExecute(result);
		}
	}

	public class ImageAndTextListAdapter extends ArrayAdapter<TrendsShow> {

		// private GridView listView;
		// private AsyncImageLoader asyncImageLoader;

		public ImageAndTextListAdapter(Activity activity,
				List<TrendsShow> TrendsShows, GridView listView) {
			super(activity, 0, TrendsShows);
			// this.listView = listView;
			// asyncImageLoader = new AsyncImageLoader();
		}

		public class Holder {
			public ImageView userimage;
			public TextView user;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			Activity activity = (Activity) getContext();

			// Inflate the views from XML
			View rowView = convertView;

			Holder holder;
			if (rowView == null) {
				LayoutInflater inflater = activity.getLayoutInflater();
				rowView = inflater.inflate(R.layout.gride_image_item, null);
				holder = new Holder();
				holder.user = (TextView) rowView
						.findViewById(R.id.grid_item_user);
				holder.userimage = (ImageView) rowView
						.findViewById(R.id.grid_item_image);
				rowView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			// final TrendsShow TrendsShow = getItem(position);
			TrendsShow TrendsShow = dataArray.get(position);
			if (TrendsShow != null) {
				// Log.v("adapter", TrendsShow.getusernameText());
				// Load the image and set it on the ImageView
				String imageUrl = TrendsShow.getImageUrl();
				holder.userimage.setTag(imageUrl);

				// Drawable cachedImage =
				// asyncImageLoader.loadDrawable(imageUrl,
				// new ImageCallback() {
				// public void imageLoaded(Drawable imageDrawable,
				// String imageUrl) {
				// ImageView imageViewByTag = (ImageView) listView
				// .findViewWithTag(imageUrl);
				// if (imageViewByTag != null) {
				// imageViewByTag
				// .setImageDrawable(imageDrawable);
				// }
				// }
				// });
				// if (cachedImage == null) {
				// holder.userimage.setImageResource(R.drawable.usericon);
				// } else {
				// holder.userimage.setImageResource(R.drawable.usericon);
				// }
				// imageView.setOnClickListener(new View.OnClickListener() {
				// @Override
				// public void onClick(View arg0) {
				// Intent intent = new Intent();
				// intent.setClass(context, UserTrendsManual.class);
				// Bundle bundle = new Bundle();
				// bundle.putString("item", TrendsShow.getusernameText());
				// bundle.putString("type", "friends");
				// intent.putExtras(bundle);
				// context.startActivity(intent);
				// }
				// });
				holder.userimage.setBackgroundResource(R.drawable.like1);
				holder.user.setText(TrendsShow.getusernameText());
			}
			return rowView;
		}
	}
}
